package com.redis.service.impl;

import com.redis.mapper.UserMapper;
import com.redis.pojo.UserDTO;
import com.redis.pojo.UserRequest;
import com.redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 用户服务
 *
 * @author : z_sz
 * @date : 2020-07-29 16:51
 **/
@Service
public class UserServiceImpl implements UserService {

    /**
     * 注入用户持久层对象
     */
    @Autowired
    UserMapper userMapper;

    /**
     * 使用@Cacheable注解，会将方法的返回值存入缓存，key的拼接方式为:UserDTO::id；
     * key的生成方式有多种，根据需求自己决定使用哪种keyGenerator
     * @param id 用户id
     * @return
     */
    @Override
    @Cacheable(value = "userDTO",key = "#id")
    public UserDTO getUser(Long id) {
        return userMapper.getUser(id);
    }

    /**
     * 使用@CacheEvict注解，写的时候会根据key值清空缓存，key的拼接方式为：userDTO::id
     * @param userRequest 用户更新实体
     * @return
     */
    @Override
    @CacheEvict(value = "userDTO",key = "#userRequest.id")
    public String updateUser(UserRequest userRequest) {
        userMapper.updateUser(userRequest);
        return "ok";
    }
}
